package com.ruoyi.asset.controller;

import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.asset.utils.SQRequestUtils;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.config.QiniuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.asset.domain.GhAssetRecord;
import com.ruoyi.asset.service.IGhAssetRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 评估记录Controller
 * 
 * @author Liu
 * @date 2025-06-23
 */
@RestController
@RequestMapping("/asset/record")
public class GhAssetRecordController extends BaseController
{
    @Autowired
    private IGhAssetRecordService ghAssetRecordService;

    @Autowired
    private QiniuService qiniuService;

    @Autowired
    private SQRequestUtils requestUtils;

    /**
     * 查询评估记录列表
     */
    @PreAuthorize("@ss.hasPermi('asset:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(GhAssetRecord ghAssetRecord)
    {
        startPage();
        List<GhAssetRecord> list = ghAssetRecordService.selectGhAssetRecordList(ghAssetRecord);
        return getDataTable(list);
    }

    /**
     * 导出评估记录列表
     */
    @PreAuthorize("@ss.hasPermi('asset:record:export')")
    @Log(title = "评估记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GhAssetRecord ghAssetRecord)
    {
        List<GhAssetRecord> list = ghAssetRecordService.selectGhAssetRecordList(ghAssetRecord);
        ExcelUtil<GhAssetRecord> util = new ExcelUtil<GhAssetRecord>(GhAssetRecord.class);
        util.exportExcel(response, list, "评估记录数据");
    }

    /**
     * 获取评估记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('asset:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(ghAssetRecordService.selectGhAssetRecordById(id));
    }

    /**
     * 新增评估记录
     */
    @PreAuthorize("@ss.hasPermi('asset:record:add')")
    @Log(title = "评估记录", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@RequestBody GhAssetRecord ghAssetRecord)
    {
        return toAjax(ghAssetRecordService.insertGhAssetRecord(ghAssetRecord));
    }

    /**
     * 修改评估记录
     */
    @PreAuthorize("@ss.hasPermi('asset:record:edit')")
    @Log(title = "评估记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GhAssetRecord ghAssetRecord)
    {
        return toAjax(ghAssetRecordService.updateGhAssetRecord(ghAssetRecord));
    }

    /**
     * 删除评估记录
     */
    @PreAuthorize("@ss.hasPermi('asset:record:remove')")
    @Log(title = "评估记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ghAssetRecordService.deleteGhAssetRecordByIds(ids));
    }

    /**
     * 获取七牛图片上传token
     *
     * @return
     */
    @GetMapping("/qiniu/token")
    public AjaxResult getQiniuToken() {
        String token = qiniuService.getToken();
        HashMap<String, String> map = new HashMap<>();
        map.put("token",token);
        map.put("url",qiniuService.getUrl());
        return AjaxResult.success(map);
    }

    /**
     * 获取狮桥车辆类型
     *
     * @return
     */
    @GetMapping("sq/carType")
    public AjaxResult getSQTemp() {
//        String str = "[{\"templateName\":\"牵引车\",\"childTemplates\":[{\"templateId\":\"YPG_QYC\",\"templateName\":\"牵引车\"}]},\n" +
//                "{\"templateName\":\"载货车\",\"childTemplates\":[{\"templateId\":\"YPG_ZHC\",\"templateName\":\"载货车\"},\n" +
//                "{\"templateId\":\"YPG_ZHC_FBHC\",\"templateName\":\"封闭货车\"},{\"templateId\":\"YPG_ZHC_XNYZHC\",\n" +
//                "\"templateName\":\"新能源载货车\"}]}]";
        JSONArray temp = requestUtils.getTemp();
        return AjaxResult.success(temp);
    }

    /**
     * 获取狮桥评估详情
     *
     * @param id
     * @return
     */
    @GetMapping("sq/detail/{id}")
    public void getSQDetail(@PathVariable("id") Long id) {
        ghAssetRecordService.getSQDetail(id);
    }



    @GetMapping("sq/temp/{code}")
    public AjaxResult getTempDetail(@PathVariable("code") String code) {
        JSONArray tempDetail = requestUtils.getTempDetail(code);
        return AjaxResult.success(tempDetail);

//        String str = "{\n" +
//                "\"templateInfo\":[\n" +
//                "{\n" +
//                "\"group\":\"基础信息\",\n" +
//                "\"items\":[\n" +
//                "{\n" +
//                "\"id\":\"vin\",\n" +
//                "\"label\":\"VIN\",\n" +
//                "\"type\":\"input:vin\",\n" +
//                "\"unit\":\"null\",\n" +
//                "\"placeholder\": \"null\",\n" +
//                "\"data\": \"null\",\n" +
//                "\"isMust\": 1,\n" +
//                "\"vcode\":\"null\"\n" +
//                "},\n" +
//                "{\n" +
//                "\"id\":\"releaseDate\",\n" +
//                "\"label\":\"出厂日期\",\n" +
//                "\"type\":\"input:date\",\n" +
//                "\"unit\":\"null\",\n" +
//                "\"placeholder\": \"null\",\n" +
//                "\"data\": \"null\",\n" +
//                "\"isMust\": 1,\n" +
//                "\"vcode\":\"null\"\n" +
//                "},\n" +
//                "{\n" +
//                "\"id\":\"chillerAlone\",\n" +
//                "\"label\":\"冷机品牌\",\n" +
//                "\"type\":\"select\",\n" +
//                "\"unit\":\"null\",\n" +
//                "\"placeholder\": \"null\",\n" +
//                "\"data\": [\n" +
//                "{\n" +
//                "\"key\":\"1\",\n" +
//                "\"value\":\"郑州凯雪\"\n" +
//                "},\n" +
//                "{\n" +
//                "\"key\":\"2\",\n" +
//                "\"value\":\"沈阳冰森\"\n" +
//                "}\n" +
//                "],\n" +
//                "\"isMust\": 1,\n" +
//                "\"vcode\":\"null\"\n" +
//                "}\n" +
//                "]\n" +
//                "},{\n" +
//                "\"group\":\"证件照片(上传任意一张\",\n" +
//                "\"items\":[{\n" +
//                "\"id\":\"registrationCertificate-1/2\",\n" +
//                "\"label\":\"登记证第1/2页\",\n" +
//                "\"type\":\"file-image\"}]\n" +
//                "},{\n" +
//                "\"group\":\"车辆照片\",\n" +
//                "\"items\":[{\n" +
//                "\"id\":\"003\",\n" +
//                "\"label\":\"左前45度\",\n" +
//                "\"type\":\"file-image\"}]\n" +
//                "},{\n" +
//                "\"group\":\"备注\",\n" +
//                "\"items\":[{\n" +
//                "\"id\":\"carIntroduction\",\n" +
//                "\"label\":\"备注\",\n" +
//                "\"type\":\"textarea\"}]\n" +
//                "}\n" +
//                "]\n" +
//                "}";
//        JSONObject jsonObject = JSONUtil.parseObj(str);
////        jsonObject.set("sdf", null);
//        return AjaxResult.success(jsonObject.getJSONArray("templateInfo"));
    }
}
